// pages/goods/goods-info.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    current: 1,

    specsNum: 0,

    paySpecs: false,

    register: false,

    gs_id: "",

    registerTo: false,

  },

  bindRegisterTo(e) {
    const that = this;
    console.log(e)
    if (that.data.registerTo == false) {
      that.setData({
        registerTo: true
      })
    } else {
      that.setData({
        registerTo: false
      })
    }
  },

  bindTips(e) {
    wx.showToast({
      title: '库存不足',
      icon: "loading",
      duration: 1200,
      mask: true,
    })
  },

  bindpaySpecs(e) {
    const that = this;
    if (that.data.paySpecs == false) {
      that.getGoodsstyle()
      that.setData({
        specs: that.data.info.style,
        paySpecs: true
      })
    } else {
      that.setData({
        paySpecs: false
      })
    }
    wx.hideLoading()
  },

  getGoodsstyle(e) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Goodsstyle',
      method: 'POST',
      data: {
        gid: that.options.gid,
      },
      success: function (res) {
        var specsInfo = res.data.info.style;
        var list = [];
        if (res.data.info.style.length != 0) {
          for (let k in res.data.info.style) {
            if (specsInfo[k].stock != 0) {
              list.push(specsInfo[k]);
            }
          }
          that.setData({
            specsInfo: list[0],
            specsNum: list[0].id,
          })
        }
        if (res.data.info) {
          that.setData({
            specs: res.data.info.style,
          })
          wx.hideLoading
        }
      },
    })
  },

  bindRegister(e) {
    const that = this;
    if (that.data.paySpecs == false) {
      that.setData({
        paySpecs: true
      })
    } else {
      that.setData({
        paySpecs: false
      })
    }
  },

  bindSpecs(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var item = e.currentTarget.dataset.item;
    this.setData({
      specsNum: id,
      gs_id: id,
      specsInfo: item
    })
  },

  bindSwiper(e) {
    this.setData({
      current: e.detail.current + 1
    })
  },

  bindName(e) {
    this.setData({
      nameVal: e.detail.value.trim()
    })
  },

  bindPhone(e) {
    this.setData({
      phoneVal: e.detail.value.trim()
    })
  },

  bindUsetInfo(e) {
    const that = this;
    if (e.detail.userInfo) {
      that.setData({
        userInfo: e.detail.userInfo
      })
      app.globalData.userInfo = e.detail.userInfo;
      wx.request({
        url: app.globalData.url + 'Obtain',
        data: {
          openid: app.globalData.openid,
          wx_name: app.globalData.userInfo.nickName,
          wx_img: app.globalData.userInfo.avatarUrl,
        },
        method: 'POST',
        success: function (res) {
          that.bindCart()
        },
      })
    }
  },

  bindInfo(e) {
    const that = this;
    if (e.detail.userInfo) {
      that.setData({
        userInfo: e.detail.userInfo
      })
      app.globalData.userInfo = e.detail.userInfo;
      wx.request({
        url: app.globalData.url + 'Obtain',
        data: {
          openid: app.globalData.openid,
          wx_name: app.globalData.userInfo.nickName,
          wx_img: app.globalData.userInfo.avatarUrl,
        },
        method: 'POST',
        success: function (res) {},
      })
    }
  },

  bindCart(e) {
    const that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    if (that.data.info.has_stock == 0) {
      wx.showToast({
        title: '库存不足',
        icon: "loading",
        duration: 1200,
        mask: true,
      })
      return
    } else {
      wx.request({
        url: app.globalData.url + 'Addcar',
        method: 'POST',
        data: {
          gid: that.options.gid,
          gs_id: that.data.gs_id,
          uid: app.globalData.uid,
        },
        success: function (res) {
          wx.hideLoading()
          if (res.data.info) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1200,
              mask: true,
            })
            app.globalData.buyNum = 1;
            return
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 1200,
              mask: true,
            })
          }
        },
      })
    }
  },

  bindnavTo(e) {
    const that = this;
    if (that.data.info.has_stock == 2) {
      wx.showToast({
        title: '库存不足',
        icon: "loading",
        duration: 1200,
        mask: true,
      })
      return
    } else {
      wx.navigateTo({
        url: 'goods-order?gid=' + that.data.goods.id + "&gs_id=" + that.data.gs_id,
      })
    }
  },

  goBlank(e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  goIndex(e) {
    wx.switchTab({
      url: '../index/index',
    })
  },

  navTo(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  bindObtain(e) {
    const that = this;
    var gid = e.currentTarget.dataset.gid;
    if (e.detail.userInfo) {
      that.setData({
        userInfo: e.detail.userInfo
      })
      app.globalData.userInfo = e.detail.userInfo;
      wx.request({
        url: app.globalData.url + 'Obtain',
        data: {
          openid: app.globalData.openid,
          wx_name: app.globalData.userInfo.nickName,
          wx_img: app.globalData.userInfo.avatarUrl,
        },
        method: 'POST',
        success: function (res) {
          wx.navigateTo({
            url: 'goods-order?gid=' + gid + "&gs_id=" + that.data.gs_id,
          })
        },
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    let system = app.globalData.system;
    var userInfo = app.globalData.userInfo;
    that.setData({
      system,
      userInfo: userInfo
    })

    if (app.globalData.uid) {
      needUid();
    } else {
      app.idCB = function (res) {
        needUid();
      }
    }

    function needUid() {
      wx.showLoading({
        title: '玩命加载中',
        mask: true,
      })
      wx.request({
        url: app.globalData.url + 'Goodsinfo',
        method: 'POST',
        data: {
          gid: that.options.gid,
          uid: app.globalData.uid,
        },
        success: function (res) {
          wx.hideLoading()
          if (res.data.info) {
            if (res.data.info.goods.content) {
              var content = res.data.info.goods.content
              content = content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ')
              that.setData({
                content,
              })
            }
            // that.getGoodsstyle()
            that.setData({
              info: res.data.info,
              goods: res.data.info.goods,
            })
          }
        },
      })
      if (that.options.shareNum == 1) {
        wx.request({
          url: app.globalData.url + 'BandSid',
          data: {
            uid: that.options.user_id,
            s_uid: app.globalData.uid,
          },
          method: 'POST',
          success: function (res) {
            wx.hideLoading()
          },
        })
      }

    }

    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    const that = this;
    var uid = app.globalData.uid
    var shareNum = 1
    if (res.from == 'button') {
      return {
        title: that.data.goods.goods_name,
        path: 'pages/goods/goods-info?gid=' + that.options.gid + "&user_id=" + uid + "&shareNum=" + shareNum,
        imageUrl: that.data.goods.img[0],
      }
    }
  }
})